<?php
/**
 * @version        $Id$
 * @author         master@xuewl.com
 * @copyright      Copyright (c) 2007 - 2014, Chongqing xuewl Information Technology Co., Ltd.
 * @link           http://www.xuewl.com
**/
namespace Announce\Controller;
use \Admin\Controller\InitController;
/**
*	后台公告模型
*/
class AnnounceController extends InitController{
	public function _initialize(){
		parent::_initialize();
		$this->db = D('Announce');
	}

	public function init(){
		$pagecurr = max(1,I('page',0,'intval')); 
		$pagesize = 10;
		$sqlmap = array();
		$status = I('status', -1, 'intval');
		if($status > -1) $sqlmap['status'] = $status;
		$count = $this->db->where($sqlmap)->count();
		$infos = $this->db->order('dateline DESC')->where($sqlmap)->page($pagecurr,$pagesize)->select();
		$pages = page($count,$pagesize);//分页显示
		$big_menu = array('javascript:window.top.art.dialog({id:\'add\',iframe:\''.U('add').'\', title:\''.L('announce_add').'\', width:\'700\', height:\'500\', lock:true}, function(){var d = window.top.art.dialog({id:\'add\'}).data.iframe;var form = d.document.getElementById(\'dosubmit\');form.click();return false;}, function(){window.top.art.dialog({id:\'add\'}).close()});void(0);', L('announce_add'));
		include $this->admin_tpl('announce_list');
	}
	/**
	 * 	添加公告
	 */
	public function add() {
		if (submitcheck('dosubmit')) {
			$info = I('post.');
			$result = $this->db->update($info);
			if(!$result) {
				$this->error('添加公告失败');
			}
			$this->success('添加公告成功', 'javascript:close_dialog();');
		} else {
	        $form = new \Common\Library\form();
			include $this->admin_tpl('announce_add');
		}
	}

	/**
	*	修改公告
	**/
	public function edit(){		
		$aid  = (int) I('aid');
		if($aid < 1) $this->error('参数错误');
		$data = $this->db->getByAid($aid);
		if(!$data) $this->error('公告不存在或已被删除');
		if (submitcheck('dosubmit')) {
			$info = I('post.');
			$info['starttime'] = strtotime($info['starttime']);
			$info['endtime'] = strtotime($info['endtime']);
			$result = $this->db->where(array('aid'=>$aid))->save($info);
			if(!$result) {
				$this->error($this->db->getError());
			}
			$this->success('编辑公告成功', 'javascript:close_dialog();');			
		} else {
			$form = new \Common\Library\form();
			include $this->admin_tpl('announce_edit');
		}
	}	
	/**
	 * 批量删除公告
	 */
	public function delete($aid = 0) {
		if(!isset($_POST['aid']) || empty($_POST['aid'])){
			$this->error(L('empty_error'));
		}else{
			$aid = $_POST['aid'];
			if(is_array($aid)){
				$i = 0;
				foreach ($aid as $key => $value) {
					$b[$i] = $value;
					$result = $this->db->where(array('aid'=>$b[$i]))->delete();
					$i++;
				}
				$this->success('操作成功');
			}else if($aid){
				$result = $this->db->where(array('aid'=>$aid))->delete();
				$this->success('操作成功');
			}else{
				$this->error(L('empty_error'));
			}
		}
	}
}